from django.test import TestCase

# Create your tests here.
import jwt
import datetime
# from django.conf import settings
from study_drf import settings


def create_token(payload, timeout=7 * 24 * 60):
    salt = settings.SECRET_KEY
    print(salt)
    # 构造header
    headers = {
        'type': 'jwt',
        'alg': 'HS256'
    }

    # payload,用户登录成功后，传递payload={'user_id':user.id,'username':user.name}
    # 构造payload,exp超时时间，单位分钟
    # payload['exp'] = datetime.datetime.utcnow() + datetime.timedelta(minutes=timeout)
    payload['exp'] = datetime.datetime.now() + datetime.timedelta(minutes=timeout)
    print(payload)
    token = jwt.encode(payload=payload, key=salt, algorithm="HS256", headers=headers).decode('utf-8')

    return token

print(create_token({'user_id':1,'username':'lhz'}))